import { NotFoundError, t } from 'elysia'
import { ElysiaApp } from '../index'
import crypto from 'crypto'

import { table, user } from '../schema/schema'
import { like, ne, and, count, eq, or } from 'drizzle-orm'
import { isUndefined } from 'lodash'

import { UserListPage } from '../views/user-list'

export default (app: ElysiaApp) =>
  app.get(
    '/user/list',
    async ({ db, query }) => {
      const { page = 1, pageSize = 10, name, phone } = query
      let userList = await db.select().from(table.user)
      return { userList, page, pageSize, name, phone }
    },
    {
      query: t.Object({
        page: t.Optional(t.Numeric()),
        pageSize: t.Optional(t.Numeric()),
        name: t.Optional(t.String()),
        phone: t.Optional(t.String()),
      }),
    },
  ).get(
    '/user/list/page',
      ({ db, query }) => {
      return UserListPage()
    },
    {
      query: t.Object({
        page: t.Optional(t.Numeric()),
        pageSize: t.Optional(t.Numeric()),
        name: t.Optional(t.String()),
        phone: t.Optional(t.String()),
      }),
    },
  ).get(
    '/user/listAndBlog',
    async ({ db, query }) => {
      const { page = 1, pageSize = 10, name, phone } = query
      let userList = await db.query.user.findMany({
        with: {
          userBlogs:true
        }
      })
      return { userList, page, pageSize, name, phone }
    },
    {
      query: t.Object({
        page: t.Optional(t.Numeric()),
        pageSize: t.Optional(t.Numeric()),
        name: t.Optional(t.String()),
        phone: t.Optional(t.String()),
      }),
    },
  ).get('/user/add', async ({ db, query }) => {
      let ff = db.insert(table.user).values({ username: "s", password: "f", email: 'xxx@fff.com', salt: 'ff' });
      ff.execute()
      return { flag: 'add ok' }
    })


